<template>
  <basic-container v-loading="saveLoading">
    <div style="width:100%;min-height: 50px;padding-left:5px;display: inline-block;">
      <el-container>
        <el-container>
          <el-aside width="100px">
            <el-button type="primary" @click="updateRealityLineTpl" style="float: left;">保存</el-button>
          </el-aside>
        </el-container>
      </el-container>
    </div>
    <template>
      <div style="background:#fff;height: 50px;line-height: 50px;margin-bottom: 10px">
        <div class="pull-auto"  style="text-align: center;font-size: 16px;color:#3478f5;margin-right:10px">
          <div class="block">
            产线名称：{{lineName}}
          </div>
        </div>
      </div>
      <div class="form-designer">
        <el-container style="border: #e0e0e0 solid 1px;border-top:none">
          <el-aside width="240px">
            <el-header style="z-index:100;height:50px;text-align: center;line-height: 50px;width:240px;position: absolute;background: #fff">工序</el-header>
            <div style="height:800px;margin-top:45px">
              <!--工序菜单-->
              <processMenu/>
<!--              <el-button v-for="process in pfocProcessList" @click="addProcessNode(process.processName,process.id,process.workshopSection)" style="margin: 5px 5px;width:90%" >{{process.processName}}</el-button>-->
            </div>
          </el-aside>
          <el-main class="demo" style="border-left: #e0e0e0 solid 1px;">
            <avue-flow :height="800"
                       width="100%"
                       ref="flow"
                       :option="option"
                       v-model="flowForm">
              <template #header="{node}" >
                {{(node || {}).name}}
                <i class="el-icon-delete"   style="float: right;line-height: 30px;margin-right: 2px" @click="deleteNode(node.id)"></i>
              </template>
              <template slot-scope="{node}">
                  <el-descriptions  class="margin-top"  style="width: 100%;" title="" :column="1" size="small" border >
                    <el-descriptions-item label="工段">
                      <span>{{form[node.id+`_workshopSection`]}}</span>
                    </el-descriptions-item>
                  </el-descriptions>
              </template>
            </avue-flow>

          </el-main>
        </el-container>
      </div>
    </template>
  </basic-container>
</template>

<script>
    import {getPfocProcessList } from '@/api/pfoc/pfocprocess'
    import {updateLineTpl, getObj} from '@/api/pfoc/realityproductlinetemplate'
    import processMenu from '@/views/pfoc/pfocprocess/process-menu'


    export default {
        name: "edit-line-tpl",
        props:{
            lineId:{//产线模板ID
                type:String
            },
        },
        components: {
          processMenu,
        },
        provide() {
          return {
            processAddThis: this
          }
        },
        data() {
            return {
                count: 0,
                nodeCount:0,//节点下标
                form: [],//提交时数据集合
                flowForm:'',
                pfocProcessList:[],//工序集合
                option: {
                    name: null,
                    nodeList: [],//工序节点集合
                    lineList: [],//工序各节点指向顺序集合
                },
                lineName:null,//产线名称
                saveLoading:false,//保存时遮罩开关
            };
        },
        created() {
            //获取工序
            this.getPfocProcessListData();
        },
        computed: {
            nodeList () {
                return this.option.nodeList
            }

        },
        mounted() {
            this.nodeCount = this.option.nodeList.length;//初始化节点下标

        },
        methods: {
            getRealityProductLineTpl(){
                if(!this.validatenull(this.lineId)) {
                    getObj(this.lineId).then(response => {
                        this.$refs.flow.resetPlumb();
                        var data =response.data.data;
                        //产线模板名称
                        this.lineName =data.lineName;
                        //设置反显工序
                        this.option.nodeList = JSON.parse(data.processNodes);
                        //设置反显工序指向顺序
                        this.option.lineList =  JSON.parse(data.processNodeLine);
                        //初始化节点下标
                        var n =  this.option.nodeList[ this.option.nodeList.length-1].id;
                        this.nodeCount = parseInt(n.split("node")[1])+1;

                        //显示各个工序所属工段
                        for (var i = 0; i < this.option.nodeList.length; i++) {
                            var nodeId = this.option.nodeList[i].id;
                            for(var j = 0;j< this.pfocProcessList.length;j++){
                                if( this.pfocProcessList[j].id == this.option.nodeList[i].mark){
                                    this.$set(this.form, nodeId + "_workshopSection",this.pfocProcessList[j].workshopSection);//注册动态节点-工段
                                }
                            }
                        }
                        //重新初始化flow组件
                        this.$refs.flow.init();
                    }).catch(() => {
                        this.saveLoading = false
                    })
                }
            },
            /**获取工序*/
            getPfocProcessListData(){
                getPfocProcessList().then(response => {
                    this.pfocProcessList=response.data.data;
                    //显示各个工序所属工段
                    for (var i = 0; i < this.option.nodeList.length; i++) {
                        var nodeId = this.option.nodeList[i].id;
                        for(var j = 0;j< this.pfocProcessList.length;j++){
                            if( this.pfocProcessList[j].id == this.option.nodeList[i].mark){
                                this.$set(this.form, nodeId + "_workshopSection",this.pfocProcessList[j].workshopSection);//注册动态节点-工段
                            }
                        }
                    }
                })

            },
            /**添加工序*/
            addProcessNode(name,id,workshopSection){//工序名称，工序ID
                var left = 0;
                if ((parseInt(this.nodeCount / 4))%2==0){
                  left = (this.nodeCount % 4)*300

                }else {
                  left = ((3-this.nodeCount % 4)*300)
                }
                var top = parseInt(this.nodeCount / 4)*100
                this.$refs.flow.addNode(name,id,this.nodeCount,left,top);//添加节点
                      //画线
                if(this.nodeList.length>1) {
                  var t = this;
                  var index = this.nodeList.findIndex(item => item.id === "node"+t.nodeCount);
                  var from = this.nodeList[index - 1].id;
                  var to = this.nodeList[index].id;
                  this.$nextTick((function() {
                    this.$refs.flow.drawLines({"from": from, "to": to})
                  }));
                }
                this.$set(this.form, "node"+this.nodeCount + "_processName", name);//注册动态节点-当前工序名称
                this.$set(this.form, "node"+this.nodeCount + "_processId", id);//注册动态节点-当前工序ID
                this.$set(this.form, "node"+this.nodeCount + "_workshopSection",workshopSection);//注册动态节点-工段
                this.nodeCount=this.nodeCount+1;//更新节点下标
            },
            /**删除工序*/
            deleteNode(id){
                var t = this;
                this.$confirm("确定要删除节点" + id + "?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                    closeOnClickModal: !1
                }).then((function() {
                    t.$refs.flow.deleteNode(id);
                    t.option.nodeList = t.option.nodeList;
                    t.$delete(t.form, id + "_processName");//删除动态节点-当前工序名称
                    t.$delete(t.form, id + "_processId");//删除动态节点-当前工序ID
                    t.$delete(t.form, id + "_workshopSection");//删除动态节点-工段
                })).catch((function() {}));

            },

            /**保存修改产线模板*/
            updateRealityLineTpl(){
                  if (this.validatenull(this.option.nodeList)) {
                      this.$message.warning("工序不能为空。")
                      return;
                  }

                //组装需要提交的数据
                var params = {};//参数集合
                params.id = this.lineId;//产线ID
                params.lineName = this.lineName;//产线模板名称
                params.processNodes = JSON.stringify(this.option.nodeList);//工序节点集合（其中mark字段为工序ID）
                params.processNodeLine = JSON.stringify(this.option.lineList);//工序各节点指向顺序集合

                this.saveLoading = true;
                //提交
                updateLineTpl(params).then(response => {
                    this.$message.success("修改成功。")
                    this.$emit('closeTplEdit');//关闭修改页面
                    this.saveLoading = false
                }).catch(() => {
                    this.saveLoading = false
                });

            },

        }
    };
</script>

<style scoped>
  /deep/.avue-flow__node{
    height: auto!important;
  }
  /deep/ .avue-flow__node-header {
    line-height: 30px;
  }
  /deep/.el-descriptions__title {
    font-size: 14px;
    font-weight: 400;
    margin-top: 5px;
  }



  /*以下样式是打印的时候avue表格错乱修正方式（主要是序号列和操作列固定影响的）*/
  /deep/.el-table__fixed{
    display: none!important;
  }
  /deep/.el-table__fixed-right{
    display: none!important;
  }
  /deep/.el-table__cell.is-hidden>*{visibility:visible !important;}

  /deep/ .avue-grid{
    display: none!important;
  }
  /deep/.el-main{
    background:#fff !important;
  }
  /deep/.el-card__body{
    padding: 0px!important;
  }
  /*去掉滚动条样式*/
  .demo::-webkit-scrollbar {
    display: none; /* Chrome Safari */
  }
  .demo {
    scrollbar-width: none; /* firefox */
    -ms-overflow-style: none; /* IE 10+ */
    overflow-x: hidden;
    overflow-y: auto;
  }
</style>
